package edu.gatech.cs2340.OregonTrail;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;


/**
 * ConfigurationPanel class represents the configuration screen of the application
 * @author Tyler Johnston
 * @version 1.0.1
 *
 */
public class ConfigurationPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
		private JRadioButton farmerRDB, bankerRDB, carpenterRDB;
		private Game game;
	
		private JTextField playerNameTF;
		private final ButtonGroup radioButtonGroup = new ButtonGroup();
		private final ButtonGroup radioButtonGroup2 = new ButtonGroup();
		private final ButtonGroup radioButtonGroup3 = new ButtonGroup();
		private JTextField partyMemberOneTF;
		private JTextField partyMemberTwoTF;
		private JTextField partyMemberThreeTF;
		private JTextField partyMemberFourTF;
		private JButton startButton;
		private JRadioButton rdbtnLow;
		private JRadioButton rdbtnMedium;
		private JRadioButton rdbtnHigh;
		private JRadioButton rdbtnLow_1;
		private JRadioButton rdbtnHigh_1;
		private JRadioButton rdbtnMedium_1;
		
	/**
	 * Main constructor for ConfigurationPanel
	 * @param newGame Game object to be updated with information in the configuration screen
	 */
	public ConfigurationPanel(Game newGame) {
		this.game = newGame;
		setPreferredSize(new Dimension(524, 600));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{80, 120, 120, 120, 80, 0};
		gridBagLayout.rowHeights = new int[]{50, 23, 35, 30, 30, 20, 30, 30, 30, 30, 25, 35, 30, 25, 10, 0, 25, 15, 30, 20, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblPlayerName = new JLabel("Player Name:");
		GridBagConstraints gbc_lblPlayerName = new GridBagConstraints();
		gbc_lblPlayerName.anchor = GridBagConstraints.WEST;
		gbc_lblPlayerName.insets = new Insets(0, 0, 5, 5);
		gbc_lblPlayerName.gridx = 2;
		gbc_lblPlayerName.gridy = 1;
		add(lblPlayerName, gbc_lblPlayerName);
		
		this.playerNameTF = new JTextField("Won Taek");
		GridBagConstraints gbc_playerNameTF = new GridBagConstraints();
		gbc_playerNameTF.fill = GridBagConstraints.HORIZONTAL;
		gbc_playerNameTF.insets = new Insets(0, 0, 5, 5);
		gbc_playerNameTF.gridx = 2;
		gbc_playerNameTF.gridy = 2;
		add(playerNameTF, gbc_playerNameTF);
		playerNameTF.setColumns(10);
		
		JLabel lblPartyMember = new JLabel("Party Member 1:");
		GridBagConstraints gbc_lblPartyMember = new GridBagConstraints();
		gbc_lblPartyMember.anchor = GridBagConstraints.WEST;
		gbc_lblPartyMember.insets = new Insets(0, 0, 5, 5);
		gbc_lblPartyMember.gridx = 1;
		gbc_lblPartyMember.gridy = 4;
		add(lblPartyMember, gbc_lblPartyMember);
		
		JLabel lblPartyMember_1 = new JLabel("Party Member 2:");
		GridBagConstraints gbc_lblPartyMember_1 = new GridBagConstraints();
		gbc_lblPartyMember_1.anchor = GridBagConstraints.WEST;
		gbc_lblPartyMember_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblPartyMember_1.gridx = 3;
		gbc_lblPartyMember_1.gridy = 4;
		add(lblPartyMember_1, gbc_lblPartyMember_1);
		
		partyMemberOneTF = new JTextField("Tyler");
		GridBagConstraints gbc_partyMemberOneTF = new GridBagConstraints();
		gbc_partyMemberOneTF.fill = GridBagConstraints.HORIZONTAL;
		gbc_partyMemberOneTF.insets = new Insets(0, 0, 5, 5);
		gbc_partyMemberOneTF.gridx = 1;
		gbc_partyMemberOneTF.gridy = 5;
		add(partyMemberOneTF, gbc_partyMemberOneTF);
		partyMemberOneTF.setColumns(10);
		
		partyMemberTwoTF = new JTextField("SangHun");
		GridBagConstraints gbc_partyMemberTwoTF = new GridBagConstraints();
		gbc_partyMemberTwoTF.fill = GridBagConstraints.HORIZONTAL;
		gbc_partyMemberTwoTF.insets = new Insets(0, 0, 5, 5);
		gbc_partyMemberTwoTF.gridx = 3;
		gbc_partyMemberTwoTF.gridy = 5;
		add(partyMemberTwoTF, gbc_partyMemberTwoTF);
		partyMemberTwoTF.setColumns(10);
		
		StartListener startEvent = new StartListener();
			
		JLabel lblPartyMember_2 = new JLabel("Party Member 3:");
		GridBagConstraints gbc_lblPartyMember_2 = new GridBagConstraints();
		gbc_lblPartyMember_2.anchor = GridBagConstraints.WEST;
		gbc_lblPartyMember_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblPartyMember_2.gridx = 1;
		gbc_lblPartyMember_2.gridy = 7;
		add(lblPartyMember_2, gbc_lblPartyMember_2);
		
		JLabel lblPartyMember_3 = new JLabel("Party Member 4:");
		GridBagConstraints gbc_lblPartyMember_3 = new GridBagConstraints();
		gbc_lblPartyMember_3.anchor = GridBagConstraints.WEST;
		gbc_lblPartyMember_3.insets = new Insets(0, 0, 5, 5);
		gbc_lblPartyMember_3.gridx = 3;
		gbc_lblPartyMember_3.gridy = 7;
		add(lblPartyMember_3, gbc_lblPartyMember_3);
		
		partyMemberThreeTF = new JTextField("Joshua");
		GridBagConstraints gbc_partyMemberThreeTF = new GridBagConstraints();
		gbc_partyMemberThreeTF.fill = GridBagConstraints.HORIZONTAL;
		gbc_partyMemberThreeTF.insets = new Insets(0, 0, 5, 5);
		gbc_partyMemberThreeTF.gridx = 1;
		gbc_partyMemberThreeTF.gridy = 8;
		add(partyMemberThreeTF, gbc_partyMemberThreeTF);
		partyMemberThreeTF.setColumns(10);
		
		partyMemberFourTF = new JTextField("Thomas");
		GridBagConstraints gbc_partyMemberFourTF = new GridBagConstraints();
		gbc_partyMemberFourTF.fill = GridBagConstraints.HORIZONTAL;
		gbc_partyMemberFourTF.insets = new Insets(0, 0, 5, 5);
		gbc_partyMemberFourTF.gridx = 3;
		gbc_partyMemberFourTF.gridy = 8;
		add(partyMemberFourTF, gbc_partyMemberFourTF);
		partyMemberFourTF.setColumns(10);
				
		JLabel lblPlayerProfession = new JLabel("Player Profession:");
		GridBagConstraints gbc_lblPlayerProfession = new GridBagConstraints();
		gbc_lblPlayerProfession.anchor = GridBagConstraints.WEST;
		gbc_lblPlayerProfession.insets = new Insets(0, 0, 5, 5);
		gbc_lblPlayerProfession.gridx = 1;
		gbc_lblPlayerProfession.gridy = 10;
		add(lblPlayerProfession, gbc_lblPlayerProfession);
		
		this.bankerRDB = new JRadioButton("Banker");
		bankerRDB.setSelected(true);
		radioButtonGroup.add(bankerRDB);
		GridBagConstraints gbc_bankerRDB = new GridBagConstraints();
		gbc_bankerRDB.anchor = GridBagConstraints.WEST;
		gbc_bankerRDB.insets = new Insets(0, 0, 5, 5);
		gbc_bankerRDB.gridx = 1;
		gbc_bankerRDB.gridy = 11;
		add(bankerRDB, gbc_bankerRDB);
		
		this.carpenterRDB = new JRadioButton("Carpenter");
		radioButtonGroup.add(carpenterRDB);
		GridBagConstraints gbc_carpenterRDB = new GridBagConstraints();
		gbc_carpenterRDB.anchor = GridBagConstraints.WEST;
		gbc_carpenterRDB.insets = new Insets(0, 0, 5, 5);
		gbc_carpenterRDB.gridx = 2;
		gbc_carpenterRDB.gridy = 11;
		add(carpenterRDB, gbc_carpenterRDB);
		
		this.farmerRDB = new JRadioButton("Farmer");
		radioButtonGroup.add(farmerRDB);
		GridBagConstraints gbc_farmerRDB = new GridBagConstraints();
		gbc_farmerRDB.anchor = GridBagConstraints.WEST;
		gbc_farmerRDB.insets = new Insets(0, 0, 5, 5);
		gbc_farmerRDB.gridx = 3;
		gbc_farmerRDB.gridy = 11;
		add(farmerRDB, gbc_farmerRDB);
		
		JLabel lblRations = new JLabel("Rations:");
		GridBagConstraints gbc_lblRations = new GridBagConstraints();
		gbc_lblRations.anchor = GridBagConstraints.WEST;
		gbc_lblRations.insets = new Insets(0, 0, 5, 5);
		gbc_lblRations.gridx = 1;
		gbc_lblRations.gridy = 13;
		add(lblRations, gbc_lblRations);
				
		JLabel lblPace = new JLabel("Pace:");
		GridBagConstraints gbc_lblPace = new GridBagConstraints();
		gbc_lblPace.anchor = GridBagConstraints.WEST;
		gbc_lblPace.insets = new Insets(0, 0, 5, 5);
		gbc_lblPace.gridx = 3;
		gbc_lblPace.gridy = 13;
		add(lblPace, gbc_lblPace);
			
		rdbtnLow = new JRadioButton("Low");
		GridBagConstraints gbc_rdbtnLow = new GridBagConstraints();
		gbc_rdbtnLow.anchor = GridBagConstraints.WEST;
		gbc_rdbtnLow.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnLow.gridx = 1;
		gbc_rdbtnLow.gridy = 14;
		add(rdbtnLow, gbc_rdbtnLow);
		radioButtonGroup2.add(rdbtnLow);
				
		rdbtnLow_1 = new JRadioButton("Low");
		GridBagConstraints gbc_rdbtnLow_1 = new GridBagConstraints();
		gbc_rdbtnLow_1.anchor = GridBagConstraints.WEST;
		gbc_rdbtnLow_1.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnLow_1.gridx = 3;
		gbc_rdbtnLow_1.gridy = 14;
		add(rdbtnLow_1, gbc_rdbtnLow_1);
		radioButtonGroup3.add(rdbtnLow_1);
		rdbtnLow_1.setSelected(true);
			
		rdbtnMedium = new JRadioButton("Medium");
		GridBagConstraints gbc_rdbtnMedium = new GridBagConstraints();
		gbc_rdbtnMedium.anchor = GridBagConstraints.WEST;
		gbc_rdbtnMedium.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnMedium.gridx = 1;
		gbc_rdbtnMedium.gridy = 15;
		add(rdbtnMedium, gbc_rdbtnMedium);
		radioButtonGroup2.add(rdbtnMedium);
	
		rdbtnMedium_1 = new JRadioButton("Medium");
		GridBagConstraints gbc_rdbtnMedium_1 = new GridBagConstraints();
		gbc_rdbtnMedium_1.anchor = GridBagConstraints.WEST;
		gbc_rdbtnMedium_1.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnMedium_1.gridx = 3;
		gbc_rdbtnMedium_1.gridy = 15;
		add(rdbtnMedium_1, gbc_rdbtnMedium_1);
		radioButtonGroup3.add(rdbtnMedium_1);

		rdbtnHigh = new JRadioButton("High");
		GridBagConstraints gbc_rdbtnHigh = new GridBagConstraints();
		gbc_rdbtnHigh.anchor = GridBagConstraints.WEST;
		gbc_rdbtnHigh.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnHigh.gridx = 1;
		gbc_rdbtnHigh.gridy = 16;
		add(rdbtnHigh, gbc_rdbtnHigh);
		radioButtonGroup2.add(rdbtnHigh);
		rdbtnLow.setSelected(true);
	
		rdbtnHigh_1 = new JRadioButton("High");
		GridBagConstraints gbc_rdbtnHigh_1 = new GridBagConstraints();
		gbc_rdbtnHigh_1.anchor = GridBagConstraints.WEST;
		gbc_rdbtnHigh_1.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnHigh_1.gridx = 3;
		gbc_rdbtnHigh_1.gridy = 16;
		add(rdbtnHigh_1, gbc_rdbtnHigh_1);
		radioButtonGroup3.add(rdbtnHigh_1);
		
		this.startButton = new JButton("Start");
		GridBagConstraints gbc_startButton = new GridBagConstraints();
		gbc_startButton.gridwidth = 5;
		gbc_startButton.insets = new Insets(0, 0, 5, 0);
		gbc_startButton.gridx = 0;
		gbc_startButton.gridy = 18;
		add(startButton, gbc_startButton);
		startButton.addActionListener(startEvent);
	}
	
	/**
	 * Private listener class for start button
	 * @author Won Taek Chung
	 * @version 1.0.1
	 */
	private class StartListener implements ActionListener{	
		public void actionPerformed(ActionEvent event){
			if((playerNameTF.getText().length()>0)&&(partyMemberOneTF.getText().length()>0)&&(partyMemberTwoTF.getText().length()>0)&&(partyMemberThreeTF.getText().length()>0)&&(partyMemberFourTF.getText().length()>0))
			{
				if((playerNameTF.getText().length()<20)&&(partyMemberOneTF.getText().length()<20)&&(partyMemberTwoTF.getText().length()<20)&&(partyMemberThreeTF.getText().length()<20)&&(partyMemberFourTF.getText().length()<20))
				{
					Player player = null;
		
					if(carpenterRDB.isSelected())
					{
						player = new Carpenter(playerNameTF.getText());
					}
					else if(bankerRDB.isSelected())
					{
						player = new Banker(playerNameTF.getText());
					}
					else if(farmerRDB.isSelected())
					{
						player = new Farmer(playerNameTF.getText());
					}
				
					PartyMember pm1 = new PartyMember(partyMemberOneTF.getText());
					PartyMember pm2 = new PartyMember(partyMemberTwoTF.getText());
					PartyMember pm3 = new PartyMember(partyMemberThreeTF.getText());
					PartyMember pm4 = new PartyMember(partyMemberFourTF.getText());
					Party gameParty = new Party(player, pm1,pm2,pm3,pm4);
						
							
					eRation rations = null;
					ePace pace = null;
							
					if(rdbtnLow.isSelected())
					{
						rations = eRation.low;
					}
					else if(rdbtnMedium.isSelected())
					{
						rations = eRation.medium;
					}
					else if(rdbtnHigh.isSelected())
					{
						rations = eRation.high;
					}
						
					if(rdbtnLow_1.isSelected())
					{
						pace = ePace.low;
					}
					else if(rdbtnMedium_1.isSelected())
					{
						pace = ePace.medium;
					}
					else if(rdbtnHigh_1.isSelected())
					{
						pace = ePace.high;
					}			
					game.initializeNewGame(gameParty, pace, rations);
				}
				else{
					JOptionPane.showMessageDialog(null,"One or more of your names exceeds the maximum length of 20 characters.");
				}
			}
			else{
				JOptionPane.showMessageDialog(null,"Please give every member a name.");
			}
		}
	}
}